﻿//---------------------------------------------------------------------
// Copyright (c) 2007 Digerati Technologies, LLC.  All Rights Reserved.
//
// Release under the Microsoft Public License (Ms-PL).
//---------------------------------------------------------------------

using System.IO;

namespace DigeratiTech.NDiagnostics
{
    /// <summary>
    /// This <see cref="DiagnosticTask"/> verifies that the specified folder exists.
    /// </summary>
    public class FolderTask : DiagnosticTask
    {
        #region Properties...

        /// <summary>
        /// Gets or sets the path to be validated.
        /// </summary>
        /// <value>The path to be validated.</value>
        /// <remarks>The specified path must exist for this task to succeed.</remarks>
        public string Path { get; set; }

        #endregion

        #region Constructors...

        /// <summary>
        /// Initializes a new instance of the <see cref="FolderTask"/> class.
        /// </summary>
        /// <param name="group">The <see cref="TaskGroup"/> this task belongs to.</param>
        /// <param name="name">The task name.</param>
        /// <param name="path">The path to be validated.</param>
        public FolderTask(TaskGroup group, string name, string path)
        {
            taskGroup = group;
            Name = name;
            Path = path;
            Status = TaskStatus.NotRun;
        }

        #endregion

        #region Methods...

        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if the task is successful; otherwise, <c>false</c>.
        /// </returns>
        public override bool Execute()
        {
            if (Directory.Exists(Path))
            {
                Status = TaskStatus.Completed;
                Message = string.Empty;
            }
            else
            {
                Status = TaskStatus.Error;
                Message = "Folder '" + Path + "' could not be found.";
            }

            return (Message.Length == 0);
        }

        #endregion
    }
}
